package neuronvisualizer;

import java.awt.Color;

import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;

public class Connection {
	private int connectionHead;
	private int connectionTail;
	private double connectionCharge;
	private Object3D model;
	
	private SimpleVector headLoc,tailLoc;
	private float scale;
	
	private static final int MAXTHICK = 3;
	private static final int MINTHICK = 1;
	
	public boolean deleteFlag,addFlag,visible;
	
	
	public Connection(){
		this.connectionHead = 0;
		this.connectionTail = 0;
		this.connectionCharge = 1;
	}
	public Connection(int head,int tail,double charge){
	    this.connectionHead = head;
		this.connectionTail = tail;
		this.connectionCharge = charge;
		model = null;
		deleteFlag = false;
		addFlag = false;
		visible = false;
	}
	
	public int getHead(){
		return this.connectionHead;
	}
	
	public void setHead(int h){
		this.connectionHead = h;
	}
	
	public int getTail(){
		return this.connectionTail;
	}
	
	public void setTail(int t){
		this.connectionTail = t;
	}
	
	public double getCharge(){
		return this.connectionCharge;
	}
	
	public void setCharge(double c){
		this.connectionCharge = c;
	}
	
	public Object3D getModel(){
		return model;
	}
	
	public void setParents(SimpleVector a,SimpleVector b){
		headLoc = a;
		tailLoc = b;
	}
	
	public void setScale(float c){
		scale = c;
	}
	
	public void createModel(SimpleVector headLoc, SimpleVector tailLoc,boolean outgoing){
	       
		float thickness = 0;
	   
		//HANDLES SCALE/THICKNESS OF THE CONNECTION TO SHOW CHARGE MAGNITUDE
		if(connectionCharge >=0){
			thickness = (float)((connectionCharge/Neuron.maxCharge)*MAXTHICK)+MINTHICK;
		}
		else{
			thickness = (float)((connectionCharge/Neuron.minCharge)*MAXTHICK)+MINTHICK;
		}
		
		//NEW METHOD, PYRAMID
		model = new Object3D(4);
			
        int yOffset = 0;
        if(!outgoing)
        	yOffset = 8;

        SimpleVector pyramidTip = new SimpleVector(tailLoc.x,tailLoc.y+yOffset,tailLoc.z);
        SimpleVector baseOne = new SimpleVector(headLoc.x,headLoc.y-thickness+yOffset,headLoc.z);
        SimpleVector baseTwo = new SimpleVector(headLoc.x-thickness,headLoc.y+thickness+yOffset,headLoc.z);
        SimpleVector baseThree = new SimpleVector(headLoc.x+thickness,headLoc.y+thickness+yOffset,headLoc.z);
        	
        model.addTriangle(baseOne,baseTwo,baseThree);
        model.addTriangle(baseOne,baseTwo,pyramidTip);
        model.addTriangle(baseOne,baseThree,pyramidTip);
        model.addTriangle(baseTwo,baseThree,pyramidTip);
            
		//SETS THE COLOR OF THE CONNECTION TO REPRESENT EXCITATORY/INHIBITORY
		if(connectionCharge >=0){
			model.setTexture("white");
		}
		else{
			model.setTexture("bblue");
		}   
		//SETS THE OPACITY OF THE CONNECTION TO REPRESENT INCOMING VERSUS OUTGOING CONNECTIONS
		if(!outgoing){
		    	model.setTransparency(0);
		    	model.setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
		}
		else{
			model.setAdditionalColor(Color.WHITE);
		}

    	model.setLighting(Object3D.LIGHTING_NO_LIGHTS);	
		
		model.setSelectable(Object3D.MOUSE_SELECTABLE);
		model.build();        
	}
	public void clearParents(){
		headLoc = null;
		tailLoc = null;
	}
	
	public void destroyModel(){
		model = null;
		deleteFlag = false;
		visible = false;
	}
	
	public void modelBuilt(){
		addFlag = false;
		visible = true;
	}
	
	
}
